package controllers

import (
	"github.com/astaxie/beego"
	"Fblog/models"
	"github.com/astaxie/beego/logs"
)

//首页
type TopicController struct {
	beego.Controller
}

// @router /topic [get]
func (c *TopicController) Get() {
	c.TplName = "topic.html"

	c.Data["IsTopic"] = true
	c.Data["Dir"] = beego.AppConfig.String("Dir")
}

// @router /topic [post]
func (c *TopicController) Post() {
	Title := c.Input().Get("Title")
	Category := c.Input().Get("Category")
	Lables := c.Input().Get("Lables")
	Content := c.Input().Get("Content")

	err := models.AddTopic(Title, Category, Lables, Content)

	ret := Ret{}
	if err != nil {
		logs.Info(err)
		ret.Error = 201
		ret.Message = "添加失败"
	} else {
		ret.Error = 200
		ret.Message = "添加成功"
	}

	c.Data["json"] = ret

	c.ServeJSON()
	return
}

// @router /topic/all [get]
func (c *TopicController) GetAll() {
	page := models.Page{}

	if err := c.ParseForm(&page); err != nil {
		logs.Info(err)
	}

	c.Data["json"], _ = models.GetAllTopics("", "", false, page);

	c.ServeJSON()
	return
}

//修改状态
// @router /topic/mstatus [post]
func (c *TopicController) DeleteTopic() {
	Tid := c.Input().Get("Id")
	err := models.DeleteTopic(Tid)

	ret := Ret{}
	if err != nil {
		logs.Info(err)
		ret.Error = 201
		ret.Message = "修改失败"
	} else {
		ret.Error = 200
		ret.Message = "修改成功"
	}

	c.Data["json"] = ret

	c.ServeJSON()
	return
}
//查看详情
// @router /topic/v1/:id [get]
func (c *TopicController) TopicView() {
	c.TplName = "topic_ctx.html"
	Tid := c.Ctx.Input.Param(":id")

	c.Data["Topic"], _ = models.GetTopic(Tid)
	c.Data["Dir"] = beego.AppConfig.String("Dir")
}
